﻿using System.Windows;
using System.Windows.Controls;
using XmlStudio.ViewModel.Node;
using System.Windows.Input;
using XmlStudio.Dialogs;
using XmlStudio.DataLayer;
using XmlStudio.Interfaces;
using System;
using System.Linq;

namespace XmlStudio.Resources.UserControls {
    /// <summary>
    /// Interaction logic for UcXsdAttributes.xaml
    /// </summary>
    public partial class UcXsdAttributes : UserControl {
        public static RoutedCommand RemoveAttribute = new RoutedCommand();
        public static RoutedCommand EditAttribute = new RoutedCommand();

        public IXsdModel Model { get; set; }

        public UcXsdAttributes() {
            InitializeComponent();

            this.CommandBindings.Add(new CommandBinding(UcXsdAttributes.RemoveAttribute, RemoveAttributeExecuted));
            this.CommandBindings.Add(new CommandBinding(UcXsdAttributes.EditAttribute, EditAttributeExecuted));
        }

        protected void RemoveAttributeExecuted(object sender, ExecutedRoutedEventArgs e) {
            if(e.Parameter is XsdAttributeViewModel) {
                this.Attributes.Remove(e.Parameter as XsdAttributeViewModel);
            }
        }

        protected void EditAttributeExecuted(object sender, ExecutedRoutedEventArgs e) {
            var dialog = new AddXsdAttributeDialog(this.ElementType, e.Parameter as XsdAttributeViewModel, this.Model);

            var pos = Mouse.GetPosition(this);
            var pos2 = PointToScreen(pos);
            var mousePosition = pos2;
            dialog.Left = mousePosition.X;
            dialog.Top = mousePosition.Y;

            var res = dialog.ShowDialog();
            if(res.HasValue && res.Value) {
                var attribute = dialog.Attribute;

                var attIndex = this.Attributes.IndexOf(e.Parameter as XsdAttributeViewModel);
                this.Attributes.Remove(e.Parameter as XsdAttributeViewModel);

                //var exists = from x in this.Attributes where x.LocalName == attribute.LocalName && x.NamespaceURI == attribute.NamespaceURI select x;
                //if(exists.Count() == 0) {
                this.Attributes.Add(attribute);
                //} else {
                //    throw new InvalidOperationException("Cannot add the same attribute!");
                //}
            }
        }

        /// <summary>
        /// Gets or sets the type of element being edited.
        /// </summary>
        public XsdElement ElementType { get; set; }

        public static readonly DependencyProperty AttributesProperty =
            DependencyProperty.Register("Attributes", typeof(XsdAttributeCollectionViewModel), typeof(UcXsdAttributes), new UIPropertyMetadata(new XsdAttributeCollectionViewModel()));

        public static readonly DependencyProperty ItemTemplateProperty =
            DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(UcXsdAttributes), new UIPropertyMetadata());

        public XsdAttributeCollectionViewModel Attributes {
            get { return (XsdAttributeCollectionViewModel)GetValue(AttributesProperty); }
            set { SetValue(AttributesProperty, value); }
        }

        public DataTemplate ItemTemplate {
            get { return (DataTemplate)GetValue(ItemTemplateProperty); }
            set { SetValue(ItemTemplateProperty, value); }
        }

        public static readonly DependencyProperty ReadOnlyProperty =
            DependencyProperty.Register("ReadOnly", typeof(bool), typeof(UcXsdAttributes), new UIPropertyMetadata());

        public bool ReadOnly {
            get { return (bool)GetValue(ReadOnlyProperty); }
            set { SetValue(ReadOnlyProperty, value); }
        }

        private void Button_Click(object sender, RoutedEventArgs e) {
            var dialog = new AddXsdAttributeDialog(this.ElementType, this.Model);

            //var pos = Mouse.GetPosition(this);
            //var pos2 = PointToScreen(pos);
            //var mousePosition = pos2;
            //dialog.Left = mousePosition.X;
            //dialog.Top = mousePosition.Y;

            var res = dialog.ShowDialog();
            if(res.HasValue && res.Value) {
                var attribute = dialog.Attribute;
                var exists = from x in this.Attributes where x.LocalName == attribute.LocalName && x.NamespaceURI == attribute.NamespaceURI select x;
                if(exists.Count() == 0) {
                    this.Attributes.Add(attribute);
                } else {
                    MessageBox.Show("Cannot add the same attribute twice!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); // TO DO: translate
                    //throw new InvalidOperationException("Cannot add the same attribute twice!");
                }
            }
        }
    }
}
